.img {
  position: relative;
  width: 300px;/*根据所需比例设--长*/
  overflow: hidden;
}

.img::before {
  display: block;
  content: "";
  width: 100%;/*一定要100%--长*/
  height: 0;
  margin-top: 50%;/*根据所需比例设--高*/
}

.img img {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
